package com.smh.demo1.a20;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.validation.DataBinder;

import java.util.Date;

/**
 * @author shiminghui
 * @date 2025/5/1 15:09
 * @description: TODO
 */
public class TestDataBinder {
    public static void main(String[] args) {
        MyBean myBean = new MyBean();
        // 默认走的是getter和setter方法赋值
        DataBinder dataBinder = new DataBinder(myBean);
        // 添加这行代码,可以让其直接字段赋值
        dataBinder.initDirectFieldAccess();

        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.add("age", "123");
        propertyValues.add("name", "smh");
        propertyValues.add("birthDate", "2004/10/26");
        dataBinder.bind(propertyValues);
        System.out.println(myBean);

    }

    static class MyBean {
        private int age;
        private String name;
        private Date birthDate;

        @Override
        public String toString() {
            return "MyBean{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    ", birthDate=" + birthDate +
                    '}';
        }

        /*public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Date getBirthDate() {
            return birthDate;
        }

        public void setBirthDate(Date birthDate) {
            this.birthDate = birthDate;
        }*/
    }
}
